﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
//using BigInteger;
using System.IO;

namespace Vux.Helper.Imaging
{
    public class CImageProcessing
    {
     
         public string tingiaima;
         string biengiaima;
        public  string NoiGiauAnhChuaTinMat;
         
         int dem;
         public Bitmap HamGiauTin(Bitmap bmp, string tinmat,string DiaChiDich )
         {
             DiaChiDich = NoiGiauAnhChuaTinMat;
             UnicodeEncoding a = new UnicodeEncoding();
             int dunghotboy = tinmat.Length;

             byte[] byteme = a.GetBytes(tinmat);

             byte[] mang_sau = new byte[byteme.Length * 2];
             string[] mangcheck = new string[byteme.Length];
             for (int k = 0; k < byteme.Length; k = k + 2)
             {
              //   string bien_chuyen = string.Concat(Converter(byteme[k]).Reverse());
                 int loa = Convert.ToInt16(byteme[k]);
                 string bien_chuyen = ChuyenHe10sangHe2(loa);
                 int dungcuckideptrai = bien_chuyen.Length;
                 if (dungcuckideptrai < 8)
                 {
                     for (int sd = 0; sd < (8 - dungcuckideptrai); sd++)
                     {
                         bien_chuyen = "0" + bien_chuyen;

                     }
                     string k1 = bien_chuyen.Substring(0, 4);
                     string k2 = bien_chuyen.Substring(4, 4);

                     mangcheck[k] = k1;
                     mangcheck[k + 1] = k2;
                 }
                 else
                 {
                     string k1 = bien_chuyen.Substring(0, 4);
                     string k2 = bien_chuyen.Substring(4, 4);
                     mangcheck[k] = k1;
                     mangcheck[k + 1] = k2;
                 }
             }
             string[] mang_out = new string[mangcheck.Length + 1];
             mang_out[0] = Convert.ToString(mangcheck.Length);
             //mang_out[1] = "0";
             for (int i = 0; i < mangcheck.Length; i++)
             {
                 mang_out[i + 1] = mangcheck[i];
             }
             int biendemfor = 1;
             Color giauDoDaiTin = bmp.GetPixel(0, 0);
             Color giauDoDaiTin1 = bmp.GetPixel(0, 10);
             int biendau = Convert.ToInt32(mang_out[0]);
             int checks1 = bmp.Width;
             int checks2 = bmp.Height;
             int ksd = (checks1 - 10) * (checks2 - 10) / 10;
             float ksd1 = ksd / 1024;
             if (biendau >= ksd)
             {
                 //MessageBox.Show("Độ dài tin quá dài ");
                 string k = "Ảnh chỉ giấu được tối đa ";
                 string k1 = ksd1.ToString();
                 string k2 = k + k1 + "Kb";
            //     MessageBox.Show(k2);
             }
             else
             {
                // string loveu = string.Concat(Converter(biendau).Reverse());
                 string loveu = ChuyenHe10sangHe2(biendau);
                 int dungdeptrai = loveu.Length;

                 if (dungdeptrai < 24)
                 {
                     for (int kj = 0; kj < (24 - dungdeptrai); kj++)
                     {
                         loveu = "0" + loveu;
                     }
                 }
                 string loveu1, loveu2, loveu3, loveu4, loveu5, loveu6;

                 loveu1 = loveu.Substring(0, 4);
                 loveu2 = loveu.Substring(4, 4);
                 loveu3 = loveu.Substring(8, 4);
                 loveu4 = loveu.Substring(12, 4);
                 loveu5 = loveu.Substring(16, 4);
                 loveu6 = loveu.Substring(20, 4);

                 int a_themR = HamChuyen(loveu1);
                 //int a_Teeeee = HamChuyen(loveu1);
                 int a_themG = HamChuyen(loveu2);
                 int a_themB0 = HamChuyen(loveu3);
                 int a_themR1 = HamChuyen(loveu4);
                 int a_themG1 = HamChuyen(loveu5);
                 int a_themB1 = HamChuyen(loveu6);
                 int a_red = giauDoDaiTin.R;
                 int a_green = giauDoDaiTin.G;
                 int a_blue = giauDoDaiTin.B;
                 int a_red1 = giauDoDaiTin1.R;
                 int a_green1 = giauDoDaiTin1.G;
                 int a_blue1 = giauDoDaiTin1.B;
                 int phannguyen1 = a_red / 16;
                 int phannguyen2 = a_green / 16;
                 int phannguyen3 = a_blue / 16;
                 int phannguyen4 = a_red1 / 16;
                 int phannguyen5 = a_green1 / 16;
                 int phannguyen6 = a_blue1 / 16;
                 a_red = phannguyen1 * 16 + a_themR;
                 a_green = phannguyen2 * 16 + a_themG;
                 a_blue = phannguyen3 * 16 + a_themB0;
                 a_red1 = phannguyen4 * 16 + a_themR1;
                 a_green1 = phannguyen5 * 16 + a_themG1;
                 a_blue1 = phannguyen6 * 16 + a_themB1;

                 bmp.SetPixel(0, 0, Color.FromArgb(a_red, a_green, a_blue));
                 bmp.SetPixel(0, 10, Color.FromArgb(a_red1, a_green1, a_blue1));

                 int bienco = 0;
                 for (int i = 20; i < bmp.Height - 1; i = i + 10)
                 {
                     if (bienco == 1)
                     {
                         return bmp;
                         break;
                     }
                     else
                     {

                         if (i % 10 == 0)
                         {
                             for (int j = 0; j < bmp.Width - 1; j++)
                             {
                                 if (biendemfor >= mang_out.Length)
                                 {
                                     //NoiGiauAnhChuaTinMat = NoiGiauAnhChuaTinMat + "luongthedung.bmp";
                                     // bmp.Save(NoiGiauAnhChuaTinMat);
                                     //return bmp;
                                     //MessageBox.Show("Đã giấu xong tin mật");
                                     //MessageBox.Show("Ảnh được giấu ở D:\\dunglt.bmp");
                                     //  pictureBox2.Image = datten;
                                     bienco = 1;
                                     break;
                                 }
                                 else
                                 {
                                     string bGiau_R = Convert.ToString(mang_out[biendemfor]);
                                     string bGiau_G = Convert.ToString(mang_out[biendemfor + 1]);
                                     int bGiauR = HamChuyen(bGiau_R);
                                     int bGiauG = HamChuyen(bGiau_G);
                                     Color c = bmp.GetPixel(i, j);
                                     int a_r = c.R / 16;
                                     int a_g = c.G / 16;
                                     int c_RNew = a_r * 16 + bGiauR;
                                     int c_GNew = a_g * 16 + bGiauG;
                                     if (c_RNew > 255) c_RNew = c_RNew - 16;
                                     if (c_GNew > 255) c_GNew = c_GNew - 16;
                                     bmp.SetPixel(i, j, Color.FromArgb(c_RNew, c_GNew, c.B));

                                     biendemfor = biendemfor + 2;

                                     //datten.SetPixel(i, j, Color.FromArgb(c_red, c_blue, c_green));
                                     //  datten.SetPixel (i,j,Color.FromArgb (c_a,c_red,c_green,c_blue));
                                     Color c11 = bmp.GetPixel(i, j);
                                     //           dungvip[dem] = c1.R;
                                     dem++;

                                 }


                             }
                         }

                     }
                 }
             }
             return bmp;
         }
         public void HamGiaiTin(Bitmap bmp)
         {
             Bitmap anhgiaima = bmp;
             Color thongtin = anhgiaima.GetPixel(0, 0);
             Color thongtin1 = anhgiaima.GetPixel(0, 10);
             int a_duR = thongtin.R % 16;
             int a_duG = thongtin.G % 16;
             int a_duB = thongtin.B % 16;
             int a_duR1 = thongtin1.R % 16;
             int a_duG1 = thongtin1.G % 16;
             int a_duB1 = thongtin1.B % 16;
           //  string a_v = string.Concat(ChuyenHe10sangHe2(a_duR));
             string a_adasdasd = ChuyenHe10sangHe2(a_duR);
             string a_R = ChuyenHe10sangHe2(a_duR);
             string a_G = ChuyenHe10sangHe2(a_duG);
             string a_B = ChuyenHe10sangHe2(a_duB);
             string a_R1 = ChuyenHe10sangHe2(a_duR1);
             string a_G1 = ChuyenHe10sangHe2(a_duG1);
             string a_B1 = ChuyenHe10sangHe2(a_duB1);
             int dunghotboy1 = a_R.Length;
             int dunghotboy2 = a_G.Length;
             int dunghotboy3 = a_B.Length;
             int dunghotboy4 = a_R1.Length;
             int dunghotboy5 = a_G1.Length;
             int dunghotboy6 = a_B1.Length;

             if (dunghotboy1 < 4)
             {
                 for (int sad = 0; sad < (4 - dunghotboy1); sad++)
                 {
                     a_R = "0" + a_R;
                 }
             }
             if (dunghotboy2 < 4)
             {
                 for (int sda = 0; sda < (4 - dunghotboy2); sda++)
                 {
                     a_G = "0" + a_G;
                 }
             }
             if (dunghotboy3 < 4)
             {
                 for (int sda1 = 0; sda1 < (4 - dunghotboy3); sda1++)
                 {
                     a_B = "0" + a_B;
                 }
             }
             if (dunghotboy4 < 4)
             {
                 for (int sda = 0; sda < (4 - dunghotboy4); sda++)
                 {
                     a_R1 = "0" + a_R1;
                 }
             }
             if (dunghotboy5 < 4)
             {
                 for (int sda = 0; sda < (4 - dunghotboy5); sda++)
                 {
                     a_G1 = "0" + a_G1;
                 }
             }
             if (dunghotboy6 < 4)
             {
                 for (int sda = 0; sda < (4 - dunghotboy6); sda++)
                 {
                     a_B1 = "0" + a_B1;
                 }
             }
             string bienDemSoKiTu = a_R + a_G + a_B + a_R1 + a_G1 + a_B1;
             int so = HamDaoGiaiTin(bienDemSoKiTu);
             so = so / 2;
             int bienchaytrongvongfor = 0;
             int bienhetfor = 0;
             for (int i = 20; i < anhgiaima.Height - 1; i = i + 10)
             {
                 if (bienhetfor == 1)
                 {
                     break;
                 }
                 else
                 {
                     for (int j = 0; j < anhgiaima.Width - 1; j++)
                     {
                         if (bienchaytrongvongfor == so)
                         {

                           //  MessageBox.Show("Lấy thông tin thành công");
                             // biengiaima = biengiaima.Substring(0, lanhqua);
                             bienhetfor = 1;
                             break;
                         }
                         else
                         {
                             Color mauNow = anhgiaima.GetPixel(i, j);
                             int a_red = mauNow.R % 16;
                             int a_green = mauNow.G % 16;
                             //string loveu = string.Concat(Converter(biendau).Reverse());
                             

                             string a_redNhiPhan =ChuyenHe10sangHe2(a_red);
                             string a_GreenNhiPhan = ChuyenHe10sangHe2(a_green);
                             int dungcool1 = a_redNhiPhan.Length;
                             int dungcool2 = a_GreenNhiPhan.Length;
                             if (dungcool1 < 4)
                             {
                                 for (int baby = 0; baby < (4 - dungcool1); baby++)
                                 {
                                     a_redNhiPhan = "0" + a_redNhiPhan;
                                 }
                             }
                             if (dungcool2 < 4)
                             {
                                 for (int lovedd = 0; lovedd < (4 - dungcool2); lovedd++)
                                 {
                                     a_GreenNhiPhan = "0" + a_GreenNhiPhan;
                                 }
                             }

                             string bienchuyen = a_redNhiPhan + a_GreenNhiPhan;
                             int so2 = HamDao8GiaiTin(bienchuyen);
                             int so1 = HamDao8GiaiTin(bienchuyen);
                             byte[] ddung = new byte[2];
                             ddung[0] = Convert.ToByte(so1);
                             ddung[1] = Convert.ToByte(0);
                             UnicodeEncoding unicode = new UnicodeEncoding();
                             string s = unicode.GetString(ddung);
                             tingiaima = tingiaima + s;
                             biengiaima = biengiaima + s;
                             bienchaytrongvongfor++;
                         }

                     }
                 }
             }
         }

        
      
        
        string s;
        //public int chuyenhe2sanghe10(string sss)
        //{
        //    int z = sss.length;
        //    string[] uuu = new string[sss.length];
        //    for (int i = 0; i < z; i++)
        //    {
        //        return 1;
        //    }
        //}
        public int HamDaoGiaiTin(string k)
        {
            string [] k1 = new string [k.Length ];
             for (int i = 0; i < 24; i++)
            {
                k1[i] = k.Substring(i, 1);

            }
            double sum = 0;
            for (int i = 0; i < 24; i++)
            {
                int z = Convert.ToInt16(k1[i]);
                sum = sum + z * Math.Pow(2,Convert.ToDouble( 23 - i));
            }
            int sum1 = Convert.ToInt16(sum);
            return sum1;
        }
        public int HamDao8GiaiTin(string k)
        {
            string[] k1 = new string[k.Length];
            for (int i = 0; i < 8; i++)
            {
                k1[i] = k.Substring(i, 1);

            }
            double sum = 0;
            for (int i = 0; i < 8; i++)
            {
                int z = Convert.ToInt16(k1[i]);
                sum = sum + z * Math.Pow(2, Convert.ToDouble(7 - i));
            }
            int sum1 = Convert.ToInt16(sum);
            return sum1;
        }
        public int HamChuyen(string k)
        {
            string k1 = k.Substring(0, 1);
            string k2 = k.Substring(1, 1);
            string k3 = k.Substring(2, 1);
            string k4 = k.Substring(3, 1);
            int x1 = Convert.ToInt16(k1);
            int x2 = Convert.ToInt16(k2);
            int x3 = Convert.ToInt16(k3);
            int x4 = Convert.ToInt16(k4);
            int x = x1 * 8 + x2 * 4 + x3 * 2 + x4 * 1;
            return x;
        }
        public string ChuyenHe10sangHe2(int sohe10)
        {
            if (sohe10 == 0)
            {
                return v = ""; 
            }
            else
            {

                while (DieuKien(sohe10))
                {

                    int z = sohe10 % 2;
                    string z_conver = z.ToString();
                    s = s + z_conver;
                    sohe10 = sohe10 / 2;
                }
                string[] sad = new string[s.Length];
                for (int i = 0; i < sad.Length; i++)
                {
                    sad[i] = s.Substring(i, 1);
                }
                v = "";
                for (int i = sad.Length - 1; i >= 0; i--)
                {
                    v = v + sad[i];
                }
            }
            s = "";
            return v;
        }
        public bool DieuKien(int n)
        {
            if (n == 0) return false;
            else return true;
        }
        string v;

    }
}
